package us.wili.dev.client.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.wili.dev.client.dao.ClientCollectionMapper;
import us.wili.dev.client.model.req.JoinCollectionReq;
import us.wili.dev.client.model.rsp.ShopcartRsp;
import us.wili.dev.client.service.ClientCollectionServcie;
import us.wili.dev.client.service.ClientGoodsService;
import us.wili.dev.client.service.ClientUserService;
import us.wili.dev.model.entity.Collection;
import us.wili.dev.model.entity.Goods;
import us.wili.dev.model.entity.User;
import us.wili.dev.model.example.CollectionExample;
import us.wili.dev.service.impl.BaseCollectionServcieImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClientCollectionServcieImpl extends BaseCollectionServcieImpl implements ClientCollectionServcie {
    @Resource
    private ClientCollectionMapper clientCollectionMapper;

    @Autowired
    private ClientUserService clientUserService;

    @Autowired
    private ClientGoodsService clientGoodsService;

    @Override
    public List<ShopcartRsp> mycollection(Long id) {
        User user = clientUserService.findUser(id);
        List<Collection> orders = searcollection(user);
        List<ShopcartRsp> rsp = new ArrayList<>();
        for (Collection collection : orders) {
            Goods goods = clientGoodsService.collectiongoods(collection);
            ShopcartRsp rsp1 = ShopcartRsp.ToCollection(goods,collection);
            rsp.add(rsp1);
        }
        return rsp;
    }

    @Override
    public void deletecollection(Long id) {
        if(id!=null){
            CollectionExample example = new CollectionExample();
            example.createCriteria().andIdEqualTo(id);
            clientCollectionMapper.deleteByExample(example);
        }
    }

    @Override
    public Collection Joincollection(JoinCollectionReq req) {
        Collection collection = new Collection();
        collection.setUserid(req.getId());
        collection.setGoodsId(req.getGoodsId());
        clientCollectionMapper.insert(collection);
        return collection;
    }

    public List<Collection> searcollection(User user){
        CollectionExample example = new CollectionExample();
        example.createCriteria().andUseridEqualTo(user.getId());
        return clientCollectionMapper.selectByExample(example);
    }
}
